package com.sctu.EMSApp;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.MyinfoMangeNotesAdapter;
import com.sctu.bean.MyinfoManageNotes;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.util.SharePreferenceUtil;

import java.util.HashMap;
import java.util.List;

/**
 * Created by zhengsenwen on 2015/12/27.
 */
public class MyinfoManageNotesActivity extends BaseActivity {

    private ListView listView;
    private ImageView returnfragment;
    private TextView mytravlenotesnumber;
    private ImageView writenotes;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinfo_managetravelnotes);
        initViews();
    }


    private void initViews() {
        String id = SharePreferenceUtil.getInstance(this).getKeyData("userId");
//对返回按钮进行控制
        returnfragment = (ImageView) findViewById(R.id.activity_myinfo_manage_travel_notes_return_img);
        returnfragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyinfoManageNotesActivity.this.finish();
            }
        });

//对写游记的按钮进行监听
        writenotes = (ImageView) findViewById(R.id.activity_myinfo_manage_travel_notes_write_note_img);
        writenotes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到写游记也页面activity
                Intent intent = new Intent();
                intent.setClass(MyinfoManageNotesActivity.this, MyinfoWritenotesAcitvity.class);
                MyinfoManageNotesActivity.this.startActivity(intent);

            }
        });
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_MANAGE_TRAVLE_NOTES + id, listener, erorrlistener);
    }

    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("json:", json);

            MyinfoManageNotes myinfoManageNotes = JSON.parseObject(json, MyinfoManageNotes.class);
            final List<MyinfoManageNotes.Results> results = myinfoManageNotes.getResults();


            mytravlenotesnumber = (TextView) findViewById(R.id.activity_myinfo_manage_travel_notes_number);
            mytravlenotesnumber.setText(results.size() + "");    //强制把int类型转换成String类型


            listView = (ListView) findViewById(R.id.activity_myinfo_manage_travel_notes_list);
            listView.setAdapter(new MyinfoMangeNotesAdapter(MyinfoManageNotesActivity.this, results));

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    //跳转到游记详情页
                }
            });
        }
    };

    Response.ErrorListener erorrlistener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(MyinfoManageNotesActivity.this, "网络出错——管理游记页面", Toast.LENGTH_LONG)
                    .show();
        }
    };

}

